package com.ghxxzx.dagl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ghxxzx.dagl.entity.*;
import com.ghxxzx.dagl.exceptions.JyException;
import com.ghxxzx.dagl.mapper.DalxMapper;
import com.ghxxzx.dagl.mapper.GryfKjMapper;
import com.ghxxzx.dagl.mapper.GryfKjRgMapper;
import com.ghxxzx.dagl.mapper.RgshMapper;
import com.ghxxzx.dagl.service.DalrtwoService;
import com.ghxxzx.dagl.utils.DeleteFileUtils;
import com.ghxxzx.dagl.utils.PathUtils;
import com.ghxxzx.dagl.vo.ResultEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @Author: 刘华
 * @Date: 2020/11/18 19:45
 * @Version 1.0
 */
@Transactional
@Service
public class DalrtwoServiceImpl implements DalrtwoService {
    @Autowired
    private DalxMapper dalxMapper;
    @Autowired
    private GryfKjRgMapper gryfKjRgMapper;
    @Autowired
    private GryfKjMapper gryfKjMapper;
    @Autowired
    private RgshMapper rgshMapper;
    @Autowired
    private PathUtils pathUtils;
    @Override
    public ResultEntity dabrGryf(GryfKjRg gryfKjRg, User user) {
        if (gryfKjRg.getId() != null) {
            ResultEntity resultEntity = updateGryf(gryfKjRg);
            return resultEntity;
        }
        GryfKjRg byDabh = gryfKjRgMapper.findByDabh(gryfKjRg.getDabh());
        if (byDabh != null) {
            throw new JyException("该档案编号已入馆");
        }
        GryfKj dabh = gryfKjMapper.findByDabh(gryfKjRg.getDabh());
        if (dabh != null) {
            throw new JyException("该档案编号已录入");
        }
        gryfKjRgMapper.insert(gryfKjRg);
        Rgsh rgsh = new Rgsh(gryfKjRg.getDabh(), new Date(), null, user.getId(), user.getName(), null, null, 1, 2, null, null, null);
        rgshMapper.insert(rgsh);
        return ResultEntity.ok();
    }

    @Override
    public ResultEntity deletedaGryf(Integer id) {
        GryfKjRg gryfKjRg = gryfKjRgMapper.selectById(id);
        gryfKjRgMapper.deleteById(id);
        String dacl = pathUtils.getPath();
        String path = dacl + "/" + gryfKjRg.getTypeName() + "/" + gryfKjRg.getDabh();
        DeleteFileUtils.deleteDirectory(path);
        UpdateWrapper<Rgsh> wrapper = new UpdateWrapper<>();
        wrapper.eq("dabh", gryfKjRg.getDabh());
        rgshMapper.delete(wrapper);
        return ResultEntity.ok();
    }

    @Override
    public ResultEntity updatedaGryf(GryfKjRg gryfKjRg) {
        ResultEntity entity = updateGryf(gryfKjRg);
        return entity;
    }

//    @Override
//    public ResultEntity queryGryf(String dabh) {
//        GryfKjRg byDabh = gryfKjRgMapper.findByDabh(dabh);
//
//    }

    public ResultEntity updateGryf(GryfKjRg gryfKjRg) {
        QueryWrapper<Rgsh> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dabh",gryfKjRg.getDabh());
        Rgsh rgsh = rgshMapper.selectOne(queryWrapper);
        if (rgsh.getZt()!=1){
            throw new JyException("已提交或已不存在无法继续修改了");
        }
        GryfKjRg byId = gryfKjRgMapper.selectById(gryfKjRg.getId());
        if (!byId.getDabh().equalsIgnoreCase(gryfKjRg.getDabh())) {
            GryfKjRg byDabh = gryfKjRgMapper.findByDabh(gryfKjRg.getDabh());
            if (byDabh != null) {
                throw new JyException("该档案编号已录入");
            }
            GryfKj dabh = gryfKjMapper.findByDabh(gryfKjRg.getDabh());
            if (dabh != null) {
                throw new JyException("该档案编号已入馆");
            }

            UpdateWrapper<Rgsh> rgshUpdateWrapper = new UpdateWrapper<>();
            rgshUpdateWrapper.eq("dabh", byId.getDabh());
            rgshUpdateWrapper.set("dabh", gryfKjRg.getDabh());
            rgshMapper.update(new Rgsh(), rgshUpdateWrapper);
        }
        gryfKjRgMapper.updateById(gryfKjRg);
        return ResultEntity.ok();
    }
}
